import java.io.*;
import java.util.*;

public class Parser {
	//Config - first peer to contact with
	private String _peerName = null;
	private String _ip = null;
	private int _tcp;
	private int _udp;
	
	//Input
	private int _mss;
	private int _updateFrequency;
	private int _N;
	
	//Fields of owner peer
	private String _firstPeer = null;
	private String _firstIP = null;
	private int _firstTCP;
	private int _firstUDP;
	private String _firstPhone = null;
	private String _firstEmail = null;
	private int _firstBuffer;
	private String _firstPicName = null;
	private int _firstPicSize;
	
	//Commands
	private Queue<String> _queue = new LinkedList<String>();
	
	/* Get fields methods */
	
	public String get_peerName() {return _peerName;}

	public String get_ip() {
		return _ip;
	}

	public int get_tcp() {
		return _tcp;
	}

	public int get_udp() {
		return _udp;
	}

	public int get_mss() {
		return _mss;
	}

	public int get_updateFrequency() {
		return _updateFrequency;
	}

	public int get_N() {
		return _N;
	}

	public String get_firstPeer() {
		return _firstPeer;
	}

	public String get_firstIP() {
		return _firstIP;
	}

	public int get_firstTCP() {
		return _firstTCP;
	}

	public int get_firstUDP() {
		return _firstUDP;
	}

	public String get_firstPhone() {
		return _firstPhone;
	}

	public String get_firstEmail() {
		return _firstEmail;
	}

	public int get_firstBuffer() {
		return _firstBuffer;
	}

	public String get_firstPicName() {
		return _firstPicName;
	}

	public int get_firstPicSize() {
		return _firstPicSize;
	}

	public Queue<String> get_queue() {
		return _queue;
	}

	/* Parsing methods */
	
	private void parseInputEnvironment(BufferedReader reader) throws IOException{
		String line = null;
		String[] parsed = null;
		line = reader.readLine();
		
		while (line != null && !line.equals("%")) {
			parsed = line.split("\\$|:");
			
			if (parsed[1].equals("mss"))
				_mss = Integer.parseInt(parsed[2]);
			else if (parsed[1].equals("updatefrequency"))
				_updateFrequency = Integer.parseInt(parsed[2]) * 1000;
			else if (parsed[1].equals("N"))
				_N = Integer.parseInt(parsed[2]);
			
			if (line != null)
				line = reader.readLine();
		}
	}
	
	private void parseInputPeerData(BufferedReader reader) throws IOException{
		String line = null;
		String[] parsed = null;
		line = reader.readLine();
		
		while (line != null && !line.equals("%")) {
			parsed = line.split("\\$|:");
			
			if (parsed[1].equals("peername"))
				_firstPeer = parsed[2];
			else if (parsed[1].equals("ip"))
				_firstIP = parsed[2];
			else if (parsed[1].equals("tcp"))
				_firstTCP = Integer.parseInt(parsed[2]);
			else if (parsed[1].equals("udp"))
				_firstUDP = Integer.parseInt(parsed[2]);
			else if (parsed[1].equals("phone"))
				_firstPhone = parsed[2];
			else if (parsed[1].equals("email"))
				_firstEmail = parsed[2];
			else if (parsed[1].equals("buffer"))
				_firstBuffer = Integer.parseInt(parsed[2]);
			else if (parsed[1].equals("pic"))
				_firstPicName = parsed[2];
			else if (parsed[1].equals("picsize"))
				_firstPicSize = Integer.parseInt(parsed[2]);
			
			if (line != null)
				line = reader.readLine();
		}
	}
	
	private void parseInputCommands(BufferedReader reader) throws IOException{
		String line = null;
		
		while ((line = reader.readLine()) != null) {
			//parsed = line.split("\\&|:");
			_queue.add(line.substring(line.indexOf('&')+1, line.length()));
		}
	}
	
	public void parseInput(String fileName) {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(fileName));
			String line = reader.readLine();
			
			while (line != null) {
				if (line.equals("ENVIRONMENT"))
					parseInputEnvironment(reader);
				else if (line.equals("PEERDATA"))
					parseInputPeerData(reader);
				else if (line.equals("COMMANDS"))
					parseInputCommands(reader);
			
				if (line != null)
					line = reader.readLine();
			}
			
			reader.close();
		
		} catch (FileNotFoundException ex) {System.out.println(ex.getMessage());
			Proj350.logger.logError("Error opening input file");
			Proj350.logger.close();
			System.exit(-1);
		} catch (IOException ex) {
			Proj350.logger.logError("Reading error in input file");
			Proj350.logger.close();
			System.exit(-1);
		}
	}
	
	public void parseConfig(String fileName) {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(fileName));
			String line = null;
			
			line = reader.readLine();
			_peerName = line.substring(line.indexOf(':')+1, line.length());
			
			line = reader.readLine();
			_ip = line.substring(line.indexOf(':')+1, line.length());
			
			line = reader.readLine();
			_tcp = Integer.parseInt(line.substring(line.indexOf(':')+1, line.length()));
			
			line = reader.readLine();
			_udp = Integer.parseInt(line.substring(line.indexOf(':')+1, line.length()));
			
			reader.close();
		
		} catch (FileNotFoundException ex) {
			Proj350.logger.logError("Error opening config file");
			Proj350.logger.close();
			System.exit(-1);
		} catch (IOException ex) {
			Proj350.logger.logError("Reading error in config file");
			Proj350.logger.close();
			System.exit(-1);
		}
	}
}
